package com.wangjiyuan.shiwupai.ui.fragment.guangchi.pager;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wangjiyuan.shiwupai.R;
import com.wangjiyuan.shiwupai.adapter.EvaluationAdapter;
import com.wangjiyuan.shiwupai.adapter.FirstAdapter;
import com.wangjiyuan.shiwupai.adapter.FoodAdapter;
import com.wangjiyuan.shiwupai.base.BaseFragment;
import com.wangjiyuan.shiwupai.base.base_map.BaseMap;
import com.wangjiyuan.shiwupai.bean.guangchi.evaluation.FeedsBean;
import com.wangjiyuan.shiwupai.config.UrlConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PagerFragment extends BaseFragment implements PagerContract.PagerView {
    private static final String PAGE = "page";
    private static final String CATEGORY = "category";

    private int mPage;
    private int mCategory;
    private RecyclerView mRecycleView;
    private RecyclerView.Adapter<RecyclerView.ViewHolder> adapter;
    private List mList;

    public PagerFragment() {
    }

    public static PagerFragment newInstance(int page, int category) {
        PagerFragment fragment = new PagerFragment();
        Bundle args = new Bundle();
        args.putInt(PAGE, page);
        args.putInt(CATEGORY, category);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mPage = getArguments().getInt(PAGE);
            mCategory = getArguments().getInt(CATEGORY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_pager, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecycleView = (RecyclerView) view.findViewById(R.id.recycler_view);
        PagerPresenter pagerPresenter = new PagerPresenter(this);
        Map<String, String> map = BaseMap.baseMap();
        map.put(UrlConfig.PER, String.valueOf(10));
        map.put(UrlConfig.PAGE, String.valueOf(mPage));
        map.put(UrlConfig.CATEGORY, String.valueOf(mCategory));
        switch (mCategory) {
            case 1:
                mList = new ArrayList<>();
                adapter = new FirstAdapter(mContent, mList);
                mRecycleView.setAdapter(adapter);
                StaggeredGridLayoutManager manager1 = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
                mRecycleView.setLayoutManager(manager1);

                pagerPresenter.getFirst(map);
                break;
            case 2:
                mList = new ArrayList<>();
                adapter = new EvaluationAdapter(mContent, mList);
                mRecycleView.setAdapter(adapter);
                LinearLayoutManager manager2 = new LinearLayoutManager(mContent, LinearLayoutManager.VERTICAL, false);
                mRecycleView.setLayoutManager(manager2);
                pagerPresenter.getEvaluation(map);
                break;
            case 3:
            case 4:
                mList = new ArrayList<>();
                adapter = new FoodAdapter(mContent, mList);
                mRecycleView.setAdapter(adapter);
                LinearLayoutManager manager3 = new LinearLayoutManager(mContent, LinearLayoutManager.VERTICAL, false);
                mRecycleView.setLayoutManager(manager3);
                pagerPresenter.getFood(map);
                break;
        }
    }


    @Override
    public void getEvaluation(List<FeedsBean> bean) {
        mList.clear();
        mList.addAll(bean);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getFood(List<com.wangjiyuan.shiwupai.bean.guangchi.food.FeedsBean> bean) {
        mList.clear();
        mList.addAll(bean);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getFirst(List<com.wangjiyuan.shiwupai.bean.guangchi.first.FeedsBean> bean) {
        mList.clear();
        mList.addAll(bean);
        adapter.notifyDataSetChanged();
    }
}
